package dl.domainmodel.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import dl.domainmodel.strategy.CompleteRecognitionStrategy;
import dl.domainmodel.strategy.RecognitionStrategy;
import dl.domainmodel.strategy.ThreeWayRecognitionStrategy;
import dl.enumerator.ProductType;

public class Product {

	private static long productId = 1l;
	{
		productId++;
	}

	private Long id;
	private String name;
	private String type;
	private RecognitionStrategy recognitionStrategy;

	public static List<Product> productList = new ArrayList<Product>();

	public Product(String name, String type,
			RecognitionStrategy recognitionStrategy) {
		this.name = name;
		this.type = type;
		this.recognitionStrategy = recognitionStrategy;
		this.id = productId;
		// productList.add(this);
	}

	public static Product newWordProcessor(String name) {
		return new Product(name, ProductType.WORDPROCESSOR.getProductType(),
				new CompleteRecognitionStrategy());
	}

	public static Product newSpreadSheet(String name) {
		return new Product(name, ProductType.SPREADSHEET.getProductType(),
				new ThreeWayRecognitionStrategy(60, 90));
	}

	public static Product newDatabase(String name) {
		return new Product(name, ProductType.DATABASE.getProductType(),
				new ThreeWayRecognitionStrategy(30, 60));
	}

	public void calculateRevenueRecognitions(Contract contract) {
		recognitionStrategy.calculateRevenueRecognitions(contract);
	}

	public static Product find(Long id) {
		Iterator<Product> productItr = productList.iterator();
		while (productItr.hasNext()) {
			Product product = productItr.next();
			if (product.id.equals(id)) {
				return product;
			}
		}
		return null;
	}

	public static long save(Product product) {
		productList.add(product);
		return product.getId();
	}

	public static void delete(Long id) {
		productList.remove(find(id));
	}

	public Long getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getType() {
		return type;
	}

	public RecognitionStrategy getRecognitionStrategy() {
		return recognitionStrategy;
	}
	
	@Override
	public String toString() {
		return this.id + ":" + this.name + ":" + this.type;
	}

}
